import colors from './colors';

export default class Controller {
    params: any;
    __proto__: any;
    children: any[];
    
    constructor() {
        // 来自controller的特定方法构建Routes
        if(this.__proto__.Routes && this.__proto__.Routes.length>0) {
            this.children = this.__proto__.Routes;
        } else {
            this.children = []
        }
    }

    handle(req: any, res: any, next: Function) {
        switch(req.method.toLowerCase()) {
            case 'get':
                this.GET(req, res, next, this.params);
                break;
            case 'post':
                this.POST(req, res, next, this.params);
                break;
            case 'delete':
                this.DELETE(req, res, next, this.params);
                break;
            case 'put':
                this.PUT(req, res, next, this.params);
                break;
        }
        console.info(colors.green, '[similar-server]['+req.method.toUpperCase()+']['+new Date().toLocaleString()+']['+req.url+']');
    }

    GET(req: any, res: any, next: Function, params: any) {
        next()
    }
    POST(req: any, res: any, next: Function, params: any) {
        next()
    }
    PUT(req: any, res: any, next: Function, params: any) {
        next()
    }
    DELETE(req: any, res: any, next: Function, params: any) {
        next()
    }
};